package com.example.mobile2021606144ywr0511;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private ImageView imageView;
    private TextView tvMianContent;
    final String IMAGEPATH ="http://10.110.136.11:3000/";
    final String TEXTPATH ="http://10.110.136.11:3000/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    URL imageUr1=new URL(IMAGEPATH);
                    HttpURLConnection imageConnection=(HttpURLConnection)imageUr1.openConnection();
                    imageConnection.setRequestMethod("GET");
                    imageConnection.setConnectTimeout(5000);
                    InputStream imageIS=imageConnection.getInputStream();
                    final Bitmap bitmap= BitmapFactory.decodeStream(imageIS);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageBitmap(bitmap);
                        }
                    });
                    imageIS.close();
                    imageConnection.disconnect();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        new Thread(){
            @Override
            public void run(){
                super.run();
                try{
                    URL textUr1=new URL(TEXTPATH);
                    HttpURLConnection textConnection=(HttpURLConnection)textUr1.openConnection();
                    InputStream textIS=textConnection.getInputStream();
                    InputStreamReader isr=new InputStreamReader(textIS);
                    final BufferedReader br=new BufferedReader(isr);
                    String line=null;
                    String temp="";
                    while ((line=br.readLine())!=null){
                        temp+=line;
                        temp+="\n";
                    }
                    final String msg=temp;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvMianContent.setText(msg);
                        }
                    });
                    br.close();
                    isr.close();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        imageView = (ImageView) findViewById(R.id.imageView);
        tvMianContent = (TextView) findViewById(R.id.tv_mian_content);
    }
}